package com.zyc.mark.WaterMark.start;

import com.zyc.mark.Enum.StringEnum;
import com.zyc.mark.WaterMark.WaterMarkFactory;
import com.zyc.mark.pdf.PdfAddWaterMark;
import com.zyc.mark.util.FileUtil;
import com.zyc.mark.video.FFMPEG;
import lombok.extern.slf4j.Slf4j;
import org.apache.log4j.PropertyConfigurator;
import com.zyc.transfrom.PropertiesUtil;

import java.awt.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

/**
 * @author zyc
 */
@Slf4j
public class WaterMarkStater {
	final static String waterMarkPath = "waterMark.properties";
	final static String pdfSourceFolder = "pdf.sourceFolder";
	final static String videoSourceFolder = "video.sourceFolder";
	final static String pictureSourceFolder = "picture.sourceFolder";
	final static String SOURCE_FOLDER = "sourceFolder";
	final static String DEST_FOLDER = "destFolder";
	final static String FFMPEG_PATH = "ffmpeg_path";
	
	public WaterMarkStater() {
		// TODO Auto-generated constructor stub
	}

	/***

	 */

	public static void main(String[] args) {
/**
 * 	项目无关
 * 	 * class.getResourceAsStream()	以当前类的路径作为根节点 所以需要使用绝对路径 /
 * 	 * class.getClassLoader().getResourceAsStream()	classLoader当前路径就是根节点 所以可以使用相对路径 且不可以使用/作为文件的开始路径
 *
 * log.info(WaterMarkStater.class.getClassLoader().getResource(".").toString());
		URL resource = WaterMarkStater.class.getClassLoader().getResource("waterMark.properties");
		log.info(resource.toString());
		URL resource1 = WaterMarkStater.class.getResource("/waterMark.properties");
		log.info(resource1.toString());*/

		InputStream inputStream = WaterMarkStater.class.getResourceAsStream("/waterMark.properties");
		//不支持高级特性 $
		PropertyConfigurator.configure(inputStream);
		// System.setProperty("basePath", ".");


		String sourceFolder = PropertiesUtil
				.getProperties(WaterMarkStater.SOURCE_FOLDER, WaterMarkStater.waterMarkPath);
		String destFolder = PropertiesUtil
				.getProperties(WaterMarkStater.DEST_FOLDER, WaterMarkStater.waterMarkPath);
		WaterMarkStater waterMarkStater = new WaterMarkStater();
		//生成水印图片  D:\waterMark\destination\logo、logo.png
		String waterMarkContent =
				"zyc_" + System.currentTimeMillis() +"_" + UUID.randomUUID().toString().replaceAll("-", "");
		String waterMarkFile = waterMarkStater.createWaterMarkPic(waterMarkContent);
		//为图片打水印
		try {
			log.info("pictureAddWaterMark");
			String picSourceFolder = sourceFolder + StringEnum.FILE_SEPARATOR.getParam()+ "pic";
			String picDestFolder = destFolder + StringEnum.FILE_SEPARATOR.getParam()+ "pic";
			if (!new File(picDestFolder).exists()) {
				new File(picDestFolder).mkdirs();
			}
			if (!new File(picSourceFolder).exists()) {
				new File(picSourceFolder).mkdirs();
			}
			waterMarkStater.pictureAddWaterMark(picSourceFolder, picDestFolder, waterMarkFile);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
		try {
			log.info("pdfAddWaterMark");
			String pdfSourceFolder = sourceFolder + StringEnum.FILE_SEPARATOR.getParam()+ "source/pdf";
			String pdfDestFolder = destFolder + StringEnum.FILE_SEPARATOR.getParam()+ "source/pdf";
			if (!new File(pdfDestFolder).exists()) {
				boolean mkdirs = new File(pdfDestFolder).mkdirs();
			}
			if (!new File(pdfSourceFolder).exists()) {
				boolean mkdirs = new File(pdfSourceFolder).mkdirs();
			}
			waterMarkStater.pdfAddWaterMark(pdfSourceFolder, pdfDestFolder, waterMarkFile);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}catch (Exception e) {
			e.printStackTrace();
		}
		
		try {
			log.info("videoAddWaterMark");
			String videoSourceFolder = sourceFolder + StringEnum.FILE_SEPARATOR.getParam()+ "video";
			String videoDestFolder = destFolder + StringEnum.FILE_SEPARATOR.getParam()+ "video";
			if( ! new File(videoDestFolder).exists()) new File(videoDestFolder).mkdirs();
			if( ! new File(videoSourceFolder).exists()) new File(videoSourceFolder).mkdirs();
			waterMarkStater.videoAddWaterMark(videoSourceFolder, videoDestFolder, waterMarkFile);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void videoAddWaterMark(String sourceFolder, String destFolder, String waterMarkFile) throws Exception {
		String[] fileSuffix = {"mp4","avi","rm"};
		String FFMPEG = PropertiesUtil
				.getProperties(WaterMarkStater.FFMPEG_PATH, WaterMarkStater.waterMarkPath);
		List<File> videotureFileList = FileUtil.getFileListBySuffix(sourceFolder, fileSuffix);
		for (File videoFile : videotureFileList) {
			String srcVideoPath = videoFile.getAbsolutePath();
			String tarVideoPath = destFolder + StringEnum.FILE_SEPARATOR.getParam() + videoFile.getName();
			HashMap<String, String>dto=new HashMap<String, String>();
			dto.put("ffmpeg_path", FFMPEG);//必填
			dto.put("input_path", srcVideoPath);//必填
			dto.put("video_converted_path", tarVideoPath);//必填
			String waterMarkPngPath = waterMarkFile.replace("\\", "/");
			waterMarkPngPath = waterMarkPngPath.replace(":", "\\\\:");
			dto.put("logo", waterMarkPngPath);
			//可选(注意windows下面的logo地址前面要写4个反斜杠,如果用浏览器里面调用servlet并传参只用两个,如 d:\\:/ffmpeg/input/logo.png)
			String secondsString=  new FFMPEG().videoTransfer(dto);
			
			log.info("转换共用:"+secondsString+"秒 " + waterMarkFile +waterMarkPngPath);
		}
	}
	
	public void pdfAddWaterMark(String sourceFolder, String destFolder, String waterMarkFile) throws Exception {
		String[] fileSuffix = {"source/pdf"};
		List<File> pdftureFileList = FileUtil.getFileListBySuffix(sourceFolder, fileSuffix);
		for (File pdfFile : pdftureFileList) {
			String srcPdfPath = pdfFile.getAbsolutePath();
			String tarPdfPath = destFolder + StringEnum.FILE_SEPARATOR.getParam() + pdfFile.getName();
			new PdfAddWaterMark().addWaterMark(srcPdfPath, tarPdfPath, waterMarkFile);
		}
	}
	
	public void pictureAddWaterMark(String sourceFolder, String destFolder, String waterMarkFile) throws FileNotFoundException {
		String[] fileSuffix = {"jpg","png"};
		List<File> pictureFileList = FileUtil.getFileListBySuffix(sourceFolder, fileSuffix);
		for (File picFile : pictureFileList) {
			String srcImgPath = picFile.getAbsolutePath();
			String tarImgPath = destFolder + StringEnum.FILE_SEPARATOR.getParam() + picFile.getName();
			new WaterMarkFactory().addWaterMark(srcImgPath, tarImgPath, waterMarkFile);
		}
	}

	/**
	 * @param waterMarkContent 水印内容
	 * @return 水印生成路径
	 */


	public String createWaterMarkPic(String waterMarkContent) {
		String destFolder = PropertiesUtil
			.getProperties(WaterMarkStater.DEST_FOLDER, WaterMarkStater.waterMarkPath);
		//水印字体
		Font font = new Font("微软雅黑", Font.PLAIN, 35);
		//待存储的地址
		String tarImgPath = destFolder + StringEnum.BACK_SLANT.getParam()
				+ "logo" + StringEnum.BACK_SLANT.getParam() + "logo.png";
        Color color=new Color(200,200,200,80);
		new WaterMarkFactory().createWaterMark(tarImgPath, waterMarkContent, color,font);
		return tarImgPath;
	}
}
